package com.tevintao.marketingsystem.activity;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.tevintao.marketingsystem.R;
import com.tevintao.marketingsystem.log.LogManager;
import com.tevintao.marketingsystem.services.AccountManager;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class ActSetDevice extends AppCompatActivity implements View.OnClickListener {

    private TextView mExitName;
    private EditText mEditName;
    private Button mSetName;
    public static final String device_name_file = "/mnt/sdcard/MS/device_name";

    private android.os.Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_setting);
        mExitName = (TextView) findViewById(R.id.act_setting_exit_name);
        mEditName = (EditText) findViewById(R.id.act_setting_edit);
        mSetName = (Button) findViewById(R.id.act_setting_set);
        mSetName.setOnClickListener(this);

        new Thread(mGetDeviceName).start();
    }

    @Override
    public void onClick(View v) {
        if (v == mSetName) {
            String name = mEditName.getText().toString();
            if (TextUtils.isEmpty(name)) {
                Toast.makeText(this, "名称不能为空", Toast.LENGTH_LONG).show();
                return;
            }
            new Thread(mWriteDeviceName).start();
        }
    }

    private Runnable mGetDeviceName = new Runnable() {
        @Override
        public void run() {
            File msFile = new File(AccountManager.MM_SD_CACHE_PATH);
            if (!msFile.exists()) {
                msFile.mkdir();
            }
            File deviceNameFile = new File(msFile, AccountManager.DEVICE_FILE_NAME);
            try {
                InputStreamReader reader = new InputStreamReader(new FileInputStream(deviceNameFile));
                char[] buffer = new char[1024];
                reader.read(buffer);
                String deviceName = new String(buffer);
                LogManager.getInstance().logInfo("deviceName = " + deviceName);
                reader.close();
                mExitName.setText(deviceName);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };


    private Runnable mWriteDeviceName = new Runnable() {
        @Override
        public void run() {
            File msFile = new File(AccountManager.MM_SD_CACHE_PATH);
            if (!msFile.exists()) {
                msFile.mkdir();
            }
            File deviceNameFile = new File(msFile, AccountManager.DEVICE_FILE_NAME);
            OutputStreamWriter outputStreamWriter = null;
            try {
                String name = mEditName.getText().toString();
                outputStreamWriter = new OutputStreamWriter(new FileOutputStream(deviceNameFile));
                outputStreamWriter.write(name);
                outputStreamWriter.flush();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(ActSetDevice.this, "写入设备名称成功", Toast.LENGTH_LONG).show();
                        ActSetDevice.this.finish();
                    }
                });
                return;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (null != outputStreamWriter) {
                    try {
                        outputStreamWriter.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            handler.post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(ActSetDevice.this, "写入设备名称失败", Toast.LENGTH_LONG).show();
                }
            });


        }
    };
}
